import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { Context } from '@midwayjs/koa';
import { LeaveBill, LeavePage } from './../entity/leave.entity';
import {
  Controller,
  Inject,
  Body,
  Get,
  Post,
  Param,
  Query,
  Headers,
  Put,
} from '@midwayjs/core';

@Controller('/leave')
export class LeaveController {
  @Inject()
  ctx: Context;

  @Get('/showtime')
  async showTime() {
    return new Date().toJSON();
  }
  @Get('/showtime1/:name/:age')
  async showTime1(@Param('name') username, @Param('age') age) {
    console.log(username, age);
    return new Date().toJSON();
  }
  @Get('/showtime2')
  // async showTime2() {
  async showTime2(@Query('age') age, @Query('name') username) {
    console.log(username, age);
    // const t = this.ctx.query;
    // console.log(t);
    // console.log(t.name, t.age);
    return new Date().toJSON();
  }

  @Get('/showtime3')
  async showTime3(@Headers('User-Name') username, @Headers('Age') age) {
    console.log(username, age);
    return new Date().toJSON();
  }
  @Post('/showtime4')
  async showTime4(@Body('name') username, @Body('age') age) {
    // async showTime4(@Body() all) {
    // console.log(all);
    console.log(username, age);
    return new Date().toJSON();
  }
  @Post('/showtime5')
  async showTime5(@Body() person) {
    console.log(person);
    return new Date().toJSON();
  }
  @Get('/showtime6')
  async showTime6() {
    const cookie1 = this.ctx.headers.cookie;
    console.log(cookie1);
    const cookie2 = this.ctx.get('Cookie');
    console.log(cookie2);
    const username = this.ctx.cookies.get('name', {
      signed: false,
    });
    // const age = this.ctx.cookies.get('age');
    const age = this.ctx.cookies.get('age', {
      signed: false,
    });
    console.log(username, age);
    return new Date().toJSON();
  }
  @Get('/browser')
  async browser() {
    const r = new LeaveBill();
    r.applier = 'zhangsan';
    r.leaveTime = 3;
    r.reason = '生病了 ';
    return r;
  }
  @Post('/insert')
  async insert(@Body() leaveBill: LeaveBill) {
    console.log('用户待存储的信息：', leaveBill);
  }
  @InjectEntityModel(LeavePage)
  repo: Repository<LeavePage>;
  @Post('/insertOrm')
  async insertOrm(@Body() leavePage: LeavePage) {
    leavePage.id = 0;
    await this.repo.save(leavePage);
    console.log('用户待存储的信息：', leavePage);
    return leavePage;
  }
  // 读取待批阅报告的id  uri
  // 读取 批阅老师的身份 header token
  // 读取批阅分数和批阅评语  body json
  @Put('/report/mark/:id')
  async reportMark(
    @Param('id') id,
    @Body() message,
    @Headers('Authorization') token
  ) {
    console.log('读取待批阅报告的id:', id);
    console.log('读取 批阅老师的身份: ', token);
    console.log('读取批阅分数和批阅评语: ', message);
  }
}
